package com.rmfyzxfw.caseprocess.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.rmfyzxfw.caseprocess.mapper.StatusMapper;
import com.rmfyzxfw.caseprocess.model.po.Status;
import com.rmfyzxfw.caseprocess.service.StatusService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
* @author GYW
* @description 针对表【t_status(数据节点状态)】的数据库操作Service实现
* @createDate 2024-09-02 09:27:55
*/
@Service
public class StatusServiceImpl extends ServiceImpl<StatusMapper, Status>
    implements StatusService{

    private static StatusMapper statusMapper;

    @Autowired
    public void setStatusMapper(StatusMapper statusMapper) {
        StatusServiceImpl.statusMapper = statusMapper;
    }
    public static final LoadingCache<String, Status> statusCache;

    static {
        statusCache = CacheBuilder.newBuilder()
                //设置并发级别为8，并发级别是指可以同时写缓存的线程数
                .concurrencyLevel(8)
                //设置写缓存后5分钟过期
                .expireAfterWrite(5, TimeUnit.MINUTES)
                //设置缓存容器的初始容量为10
                .initialCapacity(10)
                //设置缓存最大容量为100，超过100之后就会按照LRU最近虽少使用算法来移除缓存项
                .maximumSize(1000)
                //设置要统计缓存的命中率
                .recordStats()
                .refreshAfterWrite(Duration.ofSeconds(30L)) // 30s刷新一次
                //build方法中可以指定CacheLoader，在缓存不存在时通过CacheLoader的实现自动加载缓存
                .build(
                        new CacheLoader<String, Status>() {
                            @NotNull
                            @Override
                            public Status load(@NotNull String key) {
                                return statusMapper.selectOne(Wrappers.<Status>lambdaQuery().eq(Status::getStatusId, key));
                            }
                        }
                );
    }
}




